package atguigu.thread.task;


import javax.xml.bind.SchemaOutputResolver;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class Demo1 {


    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(()->{
            System.out.println("1,supplyAsync");

            // 耗时
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "hello world";
        });

        future.whenCompleteAsync((result,ex)->{
            if(ex != null){

            }else{
                System.out.println("2,whenCompleteAsync:"+result);
                // 当计算结束后，走这里

                // 耗时
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        try{
            System.out.println("0,开始执行");
            future.get(10, TimeUnit.SECONDS);
            System.out.println("3,done");
        }catch (Exception e){
            future.completeExceptionally(e);
            System.out.println("4,completeExceptionally");
        }
    }
}
